package com.group4.rbac.web.controller;
import com.group4.rbac.entity.User;
import com.group4.rbac.service.UserService;
import com.study.dto.UserDTO;
import com.study.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/rbac/user")
public class UserController {

    @GetMapping("/login")
    public String login(){
        return "/login";
    }

    @Autowired
    UserService userService;

    /**
     * 用户登陆
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestBody User user){
        ResponseResult<User> login = userService.login(user); //调用service业务逻辑层
    return login;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public ResponseResult register(@RequestBody User user){
        ResponseResult register = userService.register(user);
        return register;
    }


    /**
     * 用户列表
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResponseResult userList(){
        List<User> users = userService.selectAll();
        return new ResponseResult(200,"用户列表",users);
    }



    @GetMapping("/getById/{id}")
    @ResponseBody
    public ResponseResult<UserDTO> getUserById(@PathVariable("id") Integer id){

        User userById = userService.getUserById(id);

        UserDTO userDTO = new UserDTO();

        BeanUtils.copyProperties(userById,userDTO);

        return new ResponseResult(200,"",userDTO);
    }

}

